package com.herui.iot.ui.dialog;

import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;
import com.herui.sdyu_lib.base.BaseActivity;
import com.herui.sport_care.R;
import com.herui.sport_care.data.eval.OrganTreeInfo;
import com.herui.sport_care.data.eval.TestUserInfo;
import com.herui.sport_care.ui.BaseListView;
import com.herui.sport_care.ui.evaluation.EvalPresenter;
import com.herui.sport_care.utils.ViewTools;

import java.util.LinkedList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by amanda.shan on 2022/12/14.
 */
public class UserChoseDialog {

    @BindView(R.id.view_organ)
    View view_organ;
    @BindView(R.id.tv_organ)
    TextView tv_organ;
    @BindView(R.id.id_recyclerview)
    RecyclerView id_recyclerview;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.check_all)
    CheckBox check_all;

    BaseListView<TestUserInfo> baseListView;

    private Dialog dialog;
    private BaseActivity baseActivity;
    private DialogLis dialogLis;

    private EvalPresenter evalPresenter;
    private OrganTreeInfo organTreeInfo;

    public UserChoseDialog(BaseActivity context, DialogLis dialogLis, List<TestUserInfo> all,
                           OrganTreeInfo organTreeInfo) {

        baseActivity = context;
        this.dialogLis = dialogLis;

        evalPresenter = new EvalPresenter(view);

        View layout = (View) context.getLayoutInflater().inflate(R.layout.dialog_chose_user, null);
        ButterKnife.bind(this, layout);
        dialog = new Dialog(context, R.style.MyDialogStyleBottom);
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setLayout((int) ViewTools.fromDpToPx(400), (int) ViewTools.fromDpToPx(600));
        dialogWindow.setGravity(Gravity.CENTER);

        initUi(context);

        if (organTreeInfo != null){
            tv_organ.setText(organTreeInfo.getName());
        }

        if (all != null){
            tv_organ.postDelayed(new Runnable() {
                @Override
                public void run() {
                    id_recyclerview.setVisibility(View.VISIBLE);
                    baseListView.addData(all);
                    UserChoseDialog.this.organTreeInfo = organTreeInfo;
                }
            }, 200);
        }
    }

    private void initUi(BaseActivity context) {
        id_recyclerview.setVisibility(View.INVISIBLE);
        baseListView = new BaseListView<TestUserInfo>(context) {
            @Override
            protected void convert(ViewHolder holder, TestUserInfo value, int position) {
                holder.setText(R.id.tv_phone, value.getMobile());
                holder.setText(R.id.tv_name, value.getName());
                CheckBox checkBox = holder.getView(R.id.check_chose);
                checkBox.setChecked(value.isChoseFlag());
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        value.setChoseFlag(isChecked);
                        checkAllCombox();
                    }
                });
            }

            @Override
            public void doRefresh() {
                if (organTreeInfo != null) {
                    evalPresenter.rateePageList(baseListView.getPageNumb(), organTreeInfo.getId());
                } else {
                    baseListView.loadMoreFail();
                }
            }

        };
        baseListView.initList(id_recyclerview, R.layout.item_chose_user_list, true, null);
        baseListView.enablePullRefresh(swipeRefreshLayout);

        check_all.setOnCheckedChangeListener(listener);
    }

    CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            List list = baseListView.getmDatas();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    TestUserInfo testUserInfo = (TestUserInfo) list.get(i);
                    testUserInfo.setChoseFlag(isChecked);
                }
            }
            baseListView.notifyDataSetChanged();
        }
    };

    /**
     * 检测是否需要改变全局控制的checkbox
     */
    private void checkAllCombox() {
        List<TestUserInfo> list = baseListView.getmDatas();
        boolean flag = true;
        for (TestUserInfo testUserInfo : list) {
            if (!testUserInfo.isChoseFlag()) {
                flag = false;
                break;
            }
        }
        if (list.size() == 0){
            flag = false;
        }
        check_all.setOnCheckedChangeListener(null);
        check_all.setChecked(flag);
        check_all.setOnCheckedChangeListener(listener);
    }

    @OnClick({R.id.view_organ, R.id.btn_ok, R.id.btn_cancell})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.view_organ:
                evalPresenter.getOrganTreeList();
                break;
            case R.id.btn_ok:
                List<TestUserInfo> testUserInfos = new LinkedList<>();
                List list = baseListView.getmDatas();
                if (list != null) {
                    for (int i = 0; i < list.size(); i++) {
                        TestUserInfo testUserInfo = (TestUserInfo) list.get(i);
                        if (testUserInfo.isChoseFlag()) {
                            testUserInfos.add(testUserInfo);
                        }
                    }
                }
                if (organTreeInfo != null) {
                    dialogLis.onOk(list, testUserInfos, organTreeInfo);
                }
                dialog.dismiss();
                break;
            case R.id.btn_cancell:
                dialog.dismiss();
                break;
            default:
                break;
        }

    }

    EvalPresenter.View view = new EvalPresenter.View() {
        @Override
        public void onOrganTreeInfo(OrganTreeInfo organTreeInfo) {
            List<String> collect = new LinkedList<>();
            List<OrganTreeInfo> childrenOrganList = organTreeInfo.getChildrenOrganList();
            for (OrganTreeInfo organTreeInfo1 : childrenOrganList) {
                collect.add(organTreeInfo1.getName());
            }

            new PopDialog(baseActivity, view_organ, R.layout.item_pop_eval_value, collect, "",
                    new PopDialog.SelectLis() {
                @Override
                public void onSelect(String value) {
                    for (OrganTreeInfo organTreeInfo1 : childrenOrganList) {
                        if (organTreeInfo1.getName().equals(value)) {
                            UserChoseDialog.this.organTreeInfo = organTreeInfo1;
                            tv_organ.setText(organTreeInfo1.getName());
                            id_recyclerview.setVisibility(View.VISIBLE);
                            baseListView.clearListData();
                            baseListView.doRefresh();
                            break;
                        }
                    }
                }
            });
        }

        @Override
        public void onTestUserInfo(List<TestUserInfo> testUserInfos) {
            baseListView.addData(testUserInfos);
        }
    };

    public interface DialogLis {
        void onOk(List<TestUserInfo> all, List<TestUserInfo> select, OrganTreeInfo organTreeInfo);
    }
}
